package co.fitstart.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.support.Page;
import com.demo2do.core.utils.DateUtils;

import co.fitstart.admin.dao.PrincipalDao;
import co.fitstart.admin.dao.SiteDao;
import co.fitstart.admin.service.SiteService;
import co.fitstart.entity.coach.PrincipalReportRelation;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.utils.DateFormatUtils;


@Service("siteService")
public class SiteServiceImpl implements SiteService{
    
    @Dynamic
    private SiteDao siteDao;
    
    @Dynamic
    private PrincipalDao principalDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#list(boolean, com.demo2do.core.support.Page)
     */
    public List<Site> list(boolean code, Page page) {
        List<Site> result = new ArrayList<Site>();
        if(code) {
            result = siteDao.listUnEmptyCodeSite(page.getBeginIndex(), page.getMaxResultRecords());
        } else {
            result = siteDao.listEmptyCodeSite(page.getBeginIndex(), page.getMaxResultRecords());
        }
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#load(java.lang.Long)
     */
    public Site load(Long siteId) {
        return genericDaoSupport.load(Site.class, siteId);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#edit(co.fitstart.entity.site.Site)
     */
    public void edit(Site site) {
        genericDaoSupport.update(site);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#listSite(co.fitstart.entity.principal.Principal)
     */
    public List<Site> listSite(Principal principal) {
        if(principal.isAreaManager()) {
            return siteDao.listSitesForAreaManager(principal);
        } else if(principal.isAreaServer()) {
            return siteDao.listSitesForAreaServer(principal);
        } else {
            return new ArrayList<>();
        }
        
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#loadAll()
     */
    public List<Site> loadAll() {
        return genericDaoSupport.loadAll(Site.class);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#loadAllCity()
     */
    public List<String> loadAllCity() {
        return siteDao.loadAllCity();
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#create(co.fitstart.entity.site.Site, java.lang.String, java.lang.String)
     */
    public void create(Site site, String managerName, String managerMobile) {
        if(siteDao.load(site.getName()) != null) {
            throw new BusinessException("门店名称重复");
        } else if(siteDao.loadByCode(site.getCode()) != null) {
            throw new BusinessException("门店编码重复");
        } else {
            if(principalDao.load(managerName) != null) {
                throw new BusinessException("店长账号重复");
            } else {
                //1. create site
                genericDaoSupport.save(site.initialize());
                
                //2. create site manager
                Principal manager = new Principal(managerMobile, managerName);
                
                genericDaoSupport.save(manager.initializeSiteManager().resetPassword());
                
                //3. create PrincipalReportRelation
                genericDaoSupport.save(new PrincipalReportRelation(null, manager, site));
                
                //4. edit site manager
                genericDaoSupport.update(site.editSiteManager(manager));
            }
        }
        
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#list()
     */
    public List<Site> list() {
        return siteDao.list();
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.SiteService#getSiteAllAppointTimes(java.lang.Long)
     */
    public List<String> getSiteAllAppointTime(Long siteId){
    	
    	List<String> appointTime = new ArrayList<>();
    	
    	Site site = genericDaoSupport.load(Site.class, siteId);
    	
    	String appointStartHour = site.getAppointStartHour();
    	String appointEndHour = site.getAppointEndHour();
    	
    	while (!appointStartHour.equals(appointEndHour)) {
    		
    		appointTime.add(appointStartHour);
    		Date addThirtyMinutes = org.apache.commons.lang3.time.DateUtils.addMinutes(DateUtils.parseDate(appointStartHour, "HH:mm"), 30);
    		appointStartHour = DateFormatUtils.format(addThirtyMinutes, "HH:mm");
    	}
    	
    	appointTime.add(appointEndHour);
    	
    	return appointTime;
    }
    
}
